<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 19:51:21 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1238 $
 * $Author: gavinspearhead $
 * $Id: admin_log.php 1238 2008-06-29 17:51:21Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathadl = realpath(dirname(__FILE__));

require_once "$pathadl/../functions/html_includes.php";
require_once "$pathadl/../functions/pref_functions.php";

function my_log_cmp($a, $b)
{
	global $internal_sort, $sd; /// xxx nasty!
	$sort = $internal_sort;
	return (strcasecmp ($a[$sort], $b[$sort])) * $sd;
}



if (!$isadmin)
	fatal_error($LN['error_noadmin']);

$sort = post_form('sort', 'date');
$sort_dir = $sort_dir_orig = post_form('sort_dir','desc');
$search = utf8_decode(trim(post_form('search', '')));
if ($search == html_entity_decode("<{$LN['search']}>"))
    $search = '';
$lines = post_form('lines', DEFAULT_LOG_LINES);
$default_log_level = get_config($db, 'log_level');
$min_log_level = post_form('log_level', $default_log_level);
if (!is_numeric($lines))
	fatal_error($LN['error_invalidlinescount']);

if ($lines < 0)
	$lines = DEFAULT_LOG_LINES;
else if ($lines > MAX_LOG_LINES)
	$lines = MAX_LOG_LINES;

if (FALSE === in_array($sort, array ('date', 'time', 'level', 'msg')))
	fatal_error($LN['error_unknown_sort']);

if ($sort == 'date')
	$internal_sort = 'timestamp';
elseif ($sort == 'time') 
	$internal_sort = 'timestamp2';
else 
	$internal_sort = $sort;

if (FALSE === in_array($sort_dir, array ('asc', 'desc')))
	fatal_error($LN['error_unknown_sort']);

if ($sort_dir == 'asc') 
	$sd = 1;
else 
	$sd = -1;

$error_msg = '';
$year = date('Y');
$logfile = $config['urdd_logfile'];
$logerror = URD_NOERROR;
$log = read_last_lines($logfile, $lines, $logerror, $search, $min_log_level);
$log_array = array();
if (!is_array($log)) {
	$__message[] = $LN['log_notopenlogfile'];
} else {
	if (!is_array($log) && $logerror == URD_NOERROR) {
		$logerror = URD_UNKNOWNERROR;
	} else {
		foreach ($log as $line) {
			$blocks = explode (' ', $line, 7);
			if (count($blocks) != 7) continue;
			$b['month'] = $blocks[0];
            $b['day'] = $blocks[1];
//            $b['date'] = "$year-{$blocks[0]}-{$blocks[1]}";
	 		$b['timestamp'] = strtotime("$year-{$blocks[0]}-{$blocks[1]} {$blocks[2]}");
			$b['time'] = date($LN['timeformat'], $b['timestamp']);   // $blocks[2];
            $b['date'] = date($LN['dateformat2'], $b['timestamp']);
			$b['hostname'] = $blocks[3];
			$b['proc_name'] = rtrim($blocks[4], ':');
    
			$b['timestamp2'] = "{$blocks[2]} $year-{$blocks[0]}-{$blocks[1]}";
			$b['level'] = $blocks[5];
			$b['msg'] = $blocks[6];
            $log_array[] = $b;
        }
		usort($log_array, 'my_log_cmp');
	}
	switch ($logerror) {
		case URD_NOERROR: 
			$error_msg = '';
			break;
		case URD_FILENOTFOUND:
			$error_msg = $LN['log_nofile'] . ': ' . $logfile;
			break;
		case URD_SEEKERROR:
			$error_msg = $LN['log_seekerror'];
			break;
		case URD_UNKNOWNERROR:
		default:
			$error_msg = $LN['log_unknownerror'];
	}
}

$smarty->assign('__message',	$__message);
$smarty->assign('menu',		generate_menu($LN));
$smarty->assign('title', 	'URD - ' . $LN['log_title']);
$smarty->assign('logs', $log_array);
$smarty->assign('search', $search);
$smarty->assign('lines', $lines);
$smarty->assign('logfile', $logfile);
$smarty->assign('logerrormsg', $error_msg);
$smarty->assign('log_str',		$log_str);
$smarty->assign('log_level',		$min_log_level);
$smarty->assign('sort',			$sort);
$smarty->assign('sort_dir', 		$sort_dir_orig);

$smarty->display('admin_log.tpl');



?>
